/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.pv168.uloha3.swing.workers;

import cz.muni.fi.pv168.uloha3.common.DBUtils;
import cz.muni.fi.pv168.uloha3.entities.Ownership;
import cz.muni.fi.pv168.uloha3.exceptions.OwnershipManagerException;
import cz.muni.fi.pv168.uloha3.managers.OwnerManager;
import cz.muni.fi.pv168.uloha3.managers.OwnershipManager;
import cz.muni.fi.pv168.uloha3.managers.RealEstateManager;
import cz.muni.fi.pv168.uloha3.managers.impl.OwnerManagerImpl;
import cz.muni.fi.pv168.uloha3.managers.impl.OwnershipManagerImpl;
import cz.muni.fi.pv168.uloha3.managers.impl.RealEstateManagerImpl;
import cz.muni.fi.pv168.uloha3.swing.OwnershipsTableModel;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.sql.DataSource;
import javax.swing.SwingWorker;

/**
 *
 * @author stefan
 */
public class OwnershipRemoveSwingWorker extends SwingWorker<Boolean, Void> {
    
    private Ownership ownership;
    private OwnershipsTableModel model1;
    private OwnershipsTableModel model2;

    public OwnershipRemoveSwingWorker(Ownership ownership, OwnershipsTableModel model1, OwnershipsTableModel model2) {
        this.ownership = ownership;
        this.model1 = model1;
        this.model2 = model2;
    }

    @Override
    protected Boolean doInBackground() throws Exception {
        
        DataSource dataSource = DBUtils.initConnector();
        
        OwnerManager managerOwner = new OwnerManagerImpl();
        managerOwner.setDataSource(dataSource);
        
        RealEstateManager managerRealEstate = new RealEstateManagerImpl();
        managerRealEstate.setDataSource(dataSource);
        
        OwnershipManager managerOwnership = new OwnershipManagerImpl();
        managerOwnership.setDataSource(dataSource);
        managerOwnership.setOwnerManager(managerOwner);
        managerOwnership.setRealEstateManager(managerRealEstate);
        
        try {
            managerOwnership.endOwnership(ownership);
        } catch (OwnershipManagerException ex) {
            Logger.getLogger(EstateListAllSwingWorker.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        }
        
        model1.tryRemoveOwnership(ownership);
        model2.tryRemoveOwnership(ownership);
        
        return true;
    }
    
}
